<h3>My asks</h3>
<?php
//$filter = " 1=1";
$ssql = "SELECT DISTINCT(`w`.`id`), `a`.`workid`,`a`.`id`, COUNT(DISTINCT(`w`.`id`)) AS `sF`, `a`.`finished` AS `sumfinished`,`w`.`uid`,`w`.`catid` FROM `asks` AS `A`, `works` AS `w` WHERE `finished`='0' AND `w`.`uid`='" . $_SESSION['uid'] . "' AND `w`.`id`=`a`.`workid`";
$squery = $db->query($ssql) or die(mysql_error());
$c = $db->fetch($squery);
$all = $c->sF;
$expected = $c->sumfinished;
echo "<p> I've got $expected of $all jobs rated.</p>";
// END
?>
<a id="hidefilter" style="display: none;">Hide filter</a>
<a id="showfilter" >Show filter</a> <br/>
<form id="filter" action="index.php?p=myasks" method="GET" style="display: none;">
    <input type="hidden" name="p" value="myasks"/>
    <div style="display:inline-block;">
        <table style="border:0;">
            <tr>
                <td><strong>Categories</strong>: </td>
                <td>
                    <?php
                    $db->cleanResult();
                    $db->select("categories", "*");
                    $cats = $db->getResult();
                    foreach ($cats as $cat) {
                        echo "<input name=\"cat[]\" type='checkbox' value='" . $cat['id'] . "'/> " . $cat['name'] . "<br/>";
                    }
                    ?>         
                </td>
            </tr>
        </table>
    </div>
    <div style="display:inline-block;vertical-align: top;">
        <table style="border:0;">
            <tr>
                <td>
                    <strong>Sort by</strong>:
                    <select class="span2" name="sort">
                        <option value="time">Date</option>
                        <option value="stat">Feedbacks</option>
                        <option value="cat">Category</option>
                    </select>
                </td>
            </tr>
        </table>
    </div>
    <div style="display:block;vertical-align: top;">
    <input class="btn primary" type="submit" value="Apply"/>
    </div>
</form>
<script>
    $('#showfilter').click(function() {
        $('#filter').show();
        $('#hidefilter').show();
        $('#showfilter').hide();
    });
    $('#hidefilter').click(function() {
        $('#filter').hide();
        $('#hidefilter').hide();
        $('#showfilter').show();
    });
</script>

<hr/>
<?php
$limit = 4;

if (isset($_GET['pg']))
    $cpg = $_GET['pg'];
else
    $cpg = 1;


require_once 'classes/ask.class.php';
require_once 'classes/work.class.php';

// VISI ASKAI GAUNAMI
$filter = "1=1 AND ";
/*
if (isset($_GET['status'])) {
    $stat = $_GET['status'];
    $filter .= "(";
    foreach ($stat as $s) {
        if ($s == "finish")
            $filter .= "  (COUNT*) = SELECT SUM(`a`.`finished`) AS `sumfinished` WHERE `sF` = `sumfinished`) OR";
        if ($s == "wait")
            $filter .= "  (`sF` != `sumfinished`) OR";
    }
    $filter .= " 1=1 OR";
    $filter = substr($filter,0,-3);
    $filter .= ") AND";
}
else
    $filter .= "1=1 AND";*/
if (isset($_GET['cat'])) {
    $fil = $_GET['cat'];
    $filter .= "(";
    foreach ($fil as $f) {
        $filter .= "`w`.`catid` = '$f' OR";
    }
    $filter = substr($filter,0,-3);
    $filter .= ") AND";
}


if (isset($_GET['sort'])) {
    $tmpsort = $_GET['sort'];
    if ($tmpsort == "time")
        $order = "ORDER BY `a`.`id` DESC";
    elseif ($tmpsort == "cat")
        $order = "ORDER BY `w`.`catid` DESC";
    elseif ($tmpsort == "stat")
        $order = "ORDER BY `sumfinished` DESC";
}
else {
    $order = "ORDER BY `id` DESC";
}
//$filter = " 1=1";
$sql = "SELECT DISTINCT(`a`.`workid`),`a`.`id`, COUNT(*) AS `sF`, SUM(`a`.`finished`) AS `sumfinished`,`w`.`uid`,`w`.`catid` FROM `asks` AS `A`, `works` AS `w` WHERE $filter `w`.`uid`='" . $_SESSION['uid'] . "' AND `finished`!='1' AND `w`.`id`=`a`.`workid`   GROUP BY `w`.`id` $order LIMIT 4";
$query = $db->query($sql) or die(mysql_error());
$allpages = mysql_num_rows($query);
// END

echo "<ol class=\"timeline\" id=\"updates\" style='list-style-type:none;margin:0'>";
$pg = 0;
$from = ($cpg - 1) * $limit;


//$db->select("`works`", "*", "`uid`='" . $_SESSION['uid'] . "'");
$isviso = mysql_num_rows($query);

$pages = $isviso / $limit;
if ($pages > (integer) $pages)
    $pages = $pages + 1;

//echo (integer)$pages;

while ($asks = $db->fetch($query)) {
    
    $aid = $asks->id;
    $last_ask = $aid;
    $ask = new Ask($aid);
    $wid = $ask->getWorkId();
    $sentFeedbacks = $asks->sF;
    $gotFeedbacks = $asks->sumfinished;
    $work = new Work($wid);
    require_once 'classes/category.class.php';
    $cat = new Category($work->getCategoryId());
        $datep = date("Y-m-d", strtotime(date("Y-m-d", strtotime(date("Y-m-d", strtotime($work->getDate())))) . " +2 week"));
        $datec = date("Y-m-d");
        if ($gotFeedbacks == $sentFeedbacks)
            $str = "<span class=\"label success\">Finished</span>";
        elseif ($datep > $datec) {
            if ($gotFeedbacks < $sentFeedbacks)
                $str = "<span class=\"label warning\"> Waiting for feedbacks</span>";
            if ($gotFeedbacks == 0)
                $str = "<span class=\"label important\"> No feedbacks</span>";
        }
        else
            $str = "<span class=\"label import\"> No time left</span>";
        echo "<li style='border-style:solid;border-width:1px;border-color:#DDD;padding:10px;margin:5px;background-color:#f7f7f7;display:inline-table;width: 250px;max-width: 250px;height:210px;'>";
        echo "<p style='margin:0;font-size:11px;'>" . $work->getDate() . " $str</p>";
        echo "<p style='margin:0;font-size:11px;'><strong>" . $cat->getName() . "</strong></p>";
        echo "<div style='min-height:30px;max-height:70px;height:45px;border:0;padding:0;margin:0;'><p style='line-height:15px;font-size:11px;display:block;'><i>" . substr($work->getDescription(), -100) . "...</i></p></div>";
        echo "<p style='margin:0;font-size:11px;'>Got <strong>$gotFeedbacks</strong> of <strong>$sentFeedbacks</strong> feedbacks </p>";
        $sql2 = "SELECT DISTINCT `feedbacks`.`id`, `feedbacks`.`workid`,`comment`,`feedbacks`.`uid`, AVG(`speed`) AS `speed`, AVG(`quality`) AS `quality` FROM `feedbacks` INNER JOIN `works` ON `works`.`id`=`feedbacks`.`workid` WHERE `works`.`id` = '$wid'";
        $query2 = $db->query($sql2);
        $res = $db->fetch($query2);
        $s = round($res->speed);
        $q = round($res->quality);
        ?>
        <table cellpadding="0" cellspacing="0" style="width:220px;">
            <tr style="height:30px">
                <td style="padding:5px 3px 3px 6px;margin:0;width: 100px">Speed/On Time</td>
                <td style="padding:5px 3px 3px 6px;margin:0">
                    <?php
                    for ($c = 0; $c < $s; $c++)
                        echo "<img style='width:16px;' src='star_48.png'/>";
                    $c = 0;
                    $s = 0;
                    ?>
                </td>
            </tr>
            <tr style="height:30px">
                <td style="padding:5px 3px 3px 6px;margin:0">
                    Quality 
                </td>
                <td style="padding:5px 3px 3px 6px;;margin:0">
                    <?php
                    for ($c = 0; $c < $q; $c++)
                        echo "<img style='width:16px;' src='star_48.png'/>";
                    $c = 0;
                    $q = 0;
                    ?>
                </td>
            </tr>
        </table>
        <?php
        echo "<p><a href='index.php?p=feedback&wid=$wid' style='font-size:11px;'>See detailed feedback</a></p>";
        echo "</li>";
   
}
echo "</ol>";
?>
<div id="more<?php echo $pg; ?>" class="morebox" style="padding-left:5px;">
    <input type="hidden" name="order" id="orders<?php echo $pg; ?>" value="<?php echo $order;?>"/>
    <input type="hidden" name="filter" id="filters" value="<?php echo $filter;?>"/>
<a href="#" class="btn more" id="<?php echo $pg; ?>">Show more</a>
</div>
